package com.jdew.thkj.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/6/12.
 */

public class TuiJianMingXiActivity extends BaseActivity {
    @Bind(R.id.total)
    TextView total;
    @Bind(R.id.last_super)
    TextView lastSuper;
    @Bind(R.id.last_gaoji)
    TextView lastGaoji;
    @Bind(R.id.this_super)
    TextView thisSuper;
    @Bind(R.id.this_gaoji)
    TextView thisGaoji;
    @Bind(R.id.tixian)
    TextView tixian;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    private SimpleDateFormat matter = new SimpleDateFormat("yyyy/MM");
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    private Date dt = new Date();
    private String lastdate, thisdate;
    private DecimalFormat df = new DecimalFormat( "0.00" );
    private Dialog mWeiboDialog;

    @Override
    public int bindLayout() {
        return R.layout.activity_tuijian;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        tixian.setOnClickListener(this);
        title.setText("推荐奖励明细");
        thisdate = matter.format(dt);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.add(Calendar.MONTH, -1);
        dt = calendar.getTime();
        lastdate = matter.format(dt);
    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.back:{
              finish();
          }
          break;
          case R.id.tixian:{
              Bundle b=new Bundle();
              b.putInt("type",1);
              startActivity(TiXianActivity.class,b);
          }
          break;
          default:
              break;
      }
    }

    public void GetRecommendedAwards() {
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this);
        String timesamp= RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetRecommendedAwards(sp.getString("userPhone", ""), lastdate, thisdate,RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"UserPhone"+sp.getString("userPhone", "")+"LastMonthTime"+lastdate+"ThisMonthTime"+thisdate)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {
               WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onNext(JSONObject obj) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                if(obj.getString("code").equals("0")){
                    lastSuper.setText("¥  "+obj.getString("data"));
                    lastGaoji.setText("¥  "+obj.getString("num"));
                    thisSuper.setText("¥  "+obj.getString("message"));
                    thisGaoji.setText("¥  "+obj.getString("numto"));
                    total.setText("¥ "+df.format(Double.parseDouble(obj.getString("number"))));
                    SpannableString spannableString = new SpannableString( total.getText().toString() );
                    RelativeSizeSpan sizeSpan = new RelativeSizeSpan( 1.2f );
                    spannableString.setSpan( sizeSpan, 1, total.getText().toString().indexOf( "." ), Spanned.SPAN_INCLUSIVE_EXCLUSIVE );
                    total.setText( spannableString );
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        GetRecommendedAwards();
    }
}
